package com.ruoyi.test_emp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DataMergeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.test_emp.domain.TestDept;
import com.ruoyi.test_emp.domain.TestEmp;
import com.ruoyi.test_emp.service.ITestDeptService;
import com.ruoyi.test_emp.service.ITestEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门查询Controller
 * 
 * @author ruoyi
 * @date 2021-10-03
 */
@RestController
@RequestMapping("/test_emp/dept")
public class TestDeptController extends BaseController
{
    @Autowired
    private ITestDeptService testDeptService;
    @Autowired
    private ITestEmpService iTestEmpService;

    /**
     * 查询部门查询列表
     */
    @PreAuthorize("@ss.hasPermi('test_emp:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestDept testDept)
    {
        startPage();
        List<TestDept> list = testDeptService.selectTestDeptList(testDept);
        return getDataTable(list);
    }

    /**
     * 导出部门查询列表
     */
    @PreAuthorize("@ss.hasPermi('test_emp:dept:export')")
    @Log(title = "部门查询", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TestDept testDept)
    {
        List<TestDept> list = testDeptService.selectTestDeptList(testDept);
        ExcelUtil<TestDept> util = new ExcelUtil<TestDept>(TestDept.class);
        return util.exportExcel(list, "部门查询数据");
    }

    /**
     * 获取部门查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('test_emp:dept:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(testDeptService.selectTestDeptById(id));
    }

    /**
     * 新增部门查询
     */
    @PreAuthorize("@ss.hasPermi('test_emp:dept:add')")
    @Log(title = "部门查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TestDept testDept)
    {
        return toAjax(testDeptService.insertTestDept(testDept));
    }

    /**
     * 修改部门查询
     */
    @PreAuthorize("@ss.hasPermi('test_emp:dept:edit')")
    @Log(title = "部门查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TestDept testDept)
    {
        return toAjax(testDeptService.updateTestDept(testDept));
    }

    /**
     * 删除部门查询
     */
    @PreAuthorize("@ss.hasPermi('test_emp:dept:remove')")
    @Log(title = "部门查询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(testDeptService.deleteTestDeptByIds(ids));
    }

    @GetMapping("deptTest")
    public AjaxResult deptTest(){
        List<TestDept> list = this.testDeptService.list(new LambdaQueryWrapper<TestDept>().last("limit 1000"));
        List<TestEmp> list1 = this.iTestEmpService.list(new LambdaQueryWrapper<TestEmp>()
                .last("limit 10000")
        );
        List<TestDept> testDepts = DataMergeUtils.mergeDefault(list, TestDept::getDeptno, list1, TestEmp::getDeptno);
        return AjaxResult.success(testDepts);
    }
}
